package com.qf.business.system.controller;

import com.alibaba.fastjson.JSON;
import com.qf.business.system.power.Auth;
import com.qf.business.system.service.EmployeeService;
import com.qf.data.base.r.Codes;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.system.dto.EmployeeDto;
import com.qf.data.system.entity.Employee;
import com.qf.data.system.vo.input.EmpSearchInput;
import com.qf.data.system.vo.input.EmployeeInput;
import com.qf.data.system.vo.output.EmployeeOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/emp")
@Slf4j
//@SessionAttributes("login_emp")
@Validated
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/login")
    @Auth(mustLogin = false)
    public R login(@NotBlank(message = "用户名不能为空") String username,
                   @NotBlank(message = "密码不能为空") String password,
                   HttpSession session){
        log.info("[emp login] 登录 - {} - {} - {}", username, password);
        Employee emp = employeeService.login(username, password);

        if (emp != null) {
//            model.addAttribute("login_emp", emp);
            session.setAttribute("login_emp", emp);
            //将emp转换成output对象
            EmployeeOutput employeeOutput = new EmployeeOutput();
            BeanUtils.copyProperties(emp, employeeOutput);
            return Rs.create(employeeOutput);
        }
        return Rs.createFail(Codes.LOGIN_FAIL);
    }

    /**
     * 注销登录
     * @return
     */
    @RequestMapping("/logout")
    @Auth(mustLogin = false)
    public R logout(HttpSession session){
//        Employee emp = (Employee) session.getAttribute("login_emp");
        session.removeAttribute("login_emp");
//        sessionStatus.setComplete();
        return Rs.create("succ");
    }

    /**
     * 查询所有员工 - 搜索
     * @return
     */
    @RequestMapping("/list")
    public R queryAll(EmpSearchInput search){
        List<EmployeeDto> emps = employeeService.queryBySearch(search);
        return Rs.create(emps);
    }

    /**
     * 新增员工
     * @param employeeInput
     * @return
     */
    @RequestMapping("/insert")
    public R insert(EmployeeInput employeeInput){
        //bean的拷贝
        Employee employee = QfBeanUtils.beanCopy(employeeInput, Employee.class);

        employeeService.save(employee);
        return Rs.create("succ");
    }

    /**
     * 根据员工的id 修改员工的角色
     * @param eId
     * @param rIds
     * @return
     */
    @RequestMapping("/updateRole")
    public R updateRole(Long eId, String rIds){
        List<Long> rids = JSON.parseArray(rIds, Long.class);
        employeeService.updateRoles(eId, rids);
        return Rs.create("succ");
    }
}
